package org.order;

import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.pojo.mapper.TradeOrderMapper;
import org.pojo.po.TradeOrder;
import org.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * @author hu-zhi-jun
 * @date: 2021-06-07
 */
@Component
@Slf4j
@Service(interfaceClass = OrderService.class)
public class OrderServiceImpl implements OrderService {
    @Value("${mq.order.tag.cancel}")
    private String tag;
    @Value("${mq.order.topic}")
    private String topic;

    @Autowired
    TradeOrderMapper tradeOrderMapper;

    @Autowired
    private RocketMQTemplate rocketMQTemplate;
    @Override
    public TradeOrder getOrderById(Integer orderId) {
        return tradeOrderMapper.selectById(orderId);
    }

    @Override
    public void confirmOrder() {
        try {
            sendCancelOrder(topic,tag,"1","aa");
        } catch (Exception e) {
            System.out.println(e.toString());
            e.printStackTrace();
        }

    }


    private void sendCancelOrder(String topic, String tag, String key, String body) throws Exception{
        Message message = new Message(topic,tag,key,body.getBytes());
        DefaultMQProducer producer = rocketMQTemplate.getProducer();
        producer.setVipChannelEnabled(false);
        System.out.println(producer.getNamesrvAddr());
        log.info(producer.getNamesrvAddr());
        SendResult send = producer.send(message);
        System.out.println(send);
    }

}
